package abel.wlmr.model.unit;

import abel.wlmr.model.WlmrBaseUnit;
import abel.wlmr.model.WlmrException;
import abel.wlmr.model.type.FlowPointC29Body;
import abel.wlmr.model.type.FlowUnitField;
import io.netty.buffer.ByteBuf;

public class Afn04F403Unit extends WlmrBaseUnit {
    public static final short FIXED_LEN = 2 + 5 + 2 + 5;

    private int minFlowMinutes;

    private FlowPointC29Body<FlowUnitField> minFlowThreshold = new FlowPointC29Body<>(new FlowUnitField());

    private int maxFlowMinutes;

    private FlowPointC29Body<FlowUnitField> maxFlowThreshold = new FlowPointC29Body<>(new FlowUnitField());

    public Afn04F403Unit() {
        super();
    }

    public Afn04F403Unit(short guessedLen) {
        super(guessedLen);
    }

    /* (non-Javadoc)
     * @see abel.wlmr.model.AppUnitPart#appendBuf(io.netty.buffer.ByteBuf)
     */
    @Override
    public ByteBuf appendBuf(ByteBuf buf) {
        if (minFlowMinutes < 0) {
            throw new IllegalArgumentException("min flow minutes can't be negative");
        }
        if (maxFlowMinutes < 0) {
            throw new IllegalArgumentException("max flow minutes can't be negative");
        }
        if (minFlowThreshold == null) {
            throw new IllegalArgumentException("min flow threshold can't be null");
        }
        if (maxFlowThreshold == null) {
            throw new IllegalArgumentException("max flow threshold can't be null");
        }

        buf.writeShortLE(minFlowMinutes);
        minFlowThreshold.appendBuf(buf);

        buf.writeShortLE(maxFlowMinutes);
        maxFlowThreshold.appendBuf(buf);

        return buf;
    }

    /* (non-Javadoc)
     * @see abel.wlmr.model.AppUnitPart#parseBuf(io.netty.buffer.ByteBuf)
     */
    @Override
    public int parseBuf(ByteBuf buf) throws WlmrException {
        if (checkBuffer(buf) < 0) {
            return -1;
        }

        minFlowMinutes = buf.readUnsignedShortLE();
        minFlowThreshold.parseBuf(buf);

        maxFlowMinutes = buf.readUnsignedShortLE();
        maxFlowThreshold.parseBuf(buf);

        return getGuessedLength();
    }

    /* (non-Javadoc)
     * @see abel.wlmr.model.AppUnitBody#getFinalLen()
     */
    @Override
    public short getFinalLen() {
        return FIXED_LEN;
    }

    /* (non-Javadoc)
     * @see abel.wlmr.model.WlmrDataPart#getMinLength()
     */
    @Override
    public int getMinLength() {
        return FIXED_LEN;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return String.format("{minFlowMinutes:%s, minFlowThreshold:%s, maxFlowMinutes:%s, maxFlowThreshold:%s}",
            minFlowMinutes, minFlowThreshold, maxFlowMinutes, maxFlowThreshold);
    }

    /**
     * @return the minFlowMinutes
     */
    public int getMinFlowMinutes() {
        return minFlowMinutes;
    }

    /**
     * @param minFlowMinutes
     *            the minFlowMinutes to set
     */
    public void setMinFlowMinutes(int minFlowMinutes) {
        if (minFlowMinutes < 0) {
            throw new IllegalArgumentException("min flow minutes can't be negative");
        }
        this.minFlowMinutes = minFlowMinutes;
    }

    /**
     * @return the minFlowThreshold
     */
    public FlowPointC29Body<FlowUnitField> getMinFlowThreshold() {
        return minFlowThreshold;
    }

    /**
     * @param minFlowThreshold
     *            the minFlowThreshold to set
     */
    public void setMinFlowThreshold(FlowPointC29Body<FlowUnitField> minFlowThreshold) {
        if (minFlowThreshold == null) {
            throw new IllegalArgumentException("min flow threshold can't be null");
        }
        this.minFlowThreshold = minFlowThreshold;
    }

    /**
     * @return the maxFlowMinutes
     */
    public int getMaxFlowMinutes() {
        return maxFlowMinutes;
    }

    /**
     * @param maxFlowMinutes
     *            the maxFlowMinutes to set
     */
    public void setMaxFlowMinutes(int maxFlowMinutes) {
        if (maxFlowMinutes < 0) {
            throw new IllegalArgumentException("max flow minutes can't be negative");
        }
        this.maxFlowMinutes = maxFlowMinutes;
    }

    /**
     * @return the maxFlowThreshold
     */
    public FlowPointC29Body<FlowUnitField> getMaxFlowThreshold() {
        return maxFlowThreshold;
    }

    /**
     * @param maxFlowThreshold
     *            the maxFlowThreshold to set
     */
    public void setMaxFlowThreshold(FlowPointC29Body<FlowUnitField> maxFlowThreshold) {
        if (maxFlowThreshold == null) {
            throw new IllegalArgumentException("max flow threshold can't be null");
        }
        this.maxFlowThreshold = maxFlowThreshold;
    }

}
